package com.thomas.service.impl;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.thomas.entity.Blog;
import com.thomas.entity.BlogType;
import com.thomas.entity.Blogger;
import com.thomas.entity.Link;
import com.thomas.service.BlogService;
import com.thomas.service.BlogTypeService;
import com.thomas.service.BloggerService;
import com.thomas.service.LinkService;

/** 
 * @ClassName: InitComponent 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author thomas_liu  
 * @date 2017年5月2日 下午10:14:37 
 *  
 */
@Component
public class InitComponent implements ServletContextListener ,ApplicationContextAware {
	
	private static ApplicationContext applicationContext;
	
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext application = sce.getServletContext();
		BloggerService bloggerService = (BloggerService) applicationContext.getBean("bloggerService");
		
		Blogger blogger = bloggerService.find(1);//获取博主信息
		blogger.setPassword(null);
		application.setAttribute("blogger", blogger);
		
		LinkService linkService = (LinkService) applicationContext.getBean("linkService");
		List<Link> linkList = linkService.list(null);//查询所有的友情链接信息
		application.setAttribute("linkList", linkList);
		
		BlogTypeService blogTypeService = (BlogTypeService) applicationContext.getBean("blogTypeService");
		List<BlogType> blogTypeList = blogTypeService.countList();//查询博客类别以及博客的数量
		application.setAttribute("blogTypeList", blogTypeList);
		
		BlogService blogService = (BlogService) applicationContext.getBean("blogService");
		List<Blog> blogCountList = blogService.countList();//根据日期分组查询博客
		application.setAttribute("blogCountList", blogCountList);
		
		
	}

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
	}

}
